package com.example.smallmall.controller;

import cn.hutool.Hutool;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.dto.AdminLoginParam;
import com.example.smallmall.entity.SpUser;
import com.example.smallmall.service.ISpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author WSGL
 * @since 2020-07-19
 */
@Api(tags = "用户管理")
@Controller
@RequestMapping("/users")
public class SpUserController {
    @Resource
    private ISpUserService iSpUserService;

    @ApiOperation("登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, Object>> login(@RequestBody AdminLoginParam adminLoginParam) {
        Map<String, Object> spUser = iSpUserService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword());
        if (spUser != null) {
            return CommonResult.success(spUser);
        } else {
            return CommonResult.failed("登录失败");
        }
    }

    @ApiOperation("用户数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> listUsers(@RequestParam(value = "pageNum", defaultValue = "1")
                                                       @ApiParam("页码") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "8")
                                                       @ApiParam("每页数量") Integer pageSize) {
        Map<String, Object> stringObjectMap = iSpUserService.selectPage(pageNum, pageSize);
        if (!stringObjectMap.isEmpty()) {
            return CommonResult.success(stringObjectMap);
        } else {
            return CommonResult.failed("暂无信息");
        }
    }

    @ApiOperation("添加用户信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<SpUser> createUsers(@RequestBody SpUser spUser) {
        long time = DateUtil.date().getTime();
        int nowTimeInt = Math.toIntExact((time / 1000));
        spUser.setCreateTime(nowTimeInt);
        spUser.setUpdateTime(nowTimeInt);
        spUser.setPassword("123456");
        boolean count = iSpUserService.save(spUser);
        if (count) {
            return CommonResult.success(spUser, "添加成功");
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("根据ID查询用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SpUser> listUserById(@PathVariable("id") Integer id) {
        SpUser spUser = iSpUserService.getById(id);
        if (!spUser.getUsername().isEmpty()) {
            return CommonResult.success(spUser);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("编辑用户提交")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<SpUser> updateUsers(@PathVariable("id") Integer id, @RequestBody SpUser spUser) {
        boolean isUpdate = iSpUserService.update(spUser, new QueryWrapper<SpUser>().eq("user_id", id));
        if (isUpdate) {
            SpUser user = iSpUserService.getById(id);
            return CommonResult.success(user, "更新成功");
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("根据id删除用户")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Boolean> deleteUsers(@PathVariable("id") Integer id) {
        boolean isDelete = iSpUserService.removeById(id);
        if (isDelete) {
            return CommonResult.success(true, "删除成功");
        } else {
            return CommonResult.failed("操作失败");
        }
    }
}
